<?php
// 字段信息
$keywordField = [];
$viewField = [];
foreach ($field as $key => $val) {
    if ($val['Field'] !== 'id') {
        $type = 'input';
        switch ($val['Type']) {
            case 'text':
                $type = 'editor';
                break;
            case 'datetime':
            case 'date':
                $type = $val['Type'];
                break;
        }
        $viewField[$key]['prop'] = $val['Field'];
        $viewField[$key]['label'] = $val['Field'];
        $viewField[$key]['type'] = $type;
        if ($val['Type'] !== 'datetime' 
        && $val['Type'] !== 'date' 
        && $val['Type'] !== 'int'
        && $val['Type'] !== 'tinyint') {
            array_push($keywordField, $val['Field']);
        }
    }
}

// 生成控制器
$controller = '<?php
// +----------------------------------------------------------------------
// | OneKeyAdmin [ Believe that you can do better ]
// +----------------------------------------------------------------------
// | Copyright (c) 2020-2023 http://onekeyadmin.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: MUKE <513038996@qq.com>
// +----------------------------------------------------------------------
namespace app\admin\controller;

use think\facade\View;
use think\exception\ValidateException;
use app\admin\BaseController;
use app\admin\model\\'.$class.' as '.$class.'Model;
use app\admin\validate\\'.$class.' as '.$class.'Validate;
/**
 * '.$class.'管理
 */
class '.$class.' extends BaseController
{
    /**
     * 显示资源列表
     */
    public function index()
    {
        if ($this->request->isPost()) {
            $input = input();
            $count = '.$class.'Model::withSearch(["keyword"], $input)->count();
            $data  = '.$class.'Model::withSearch(["keyword"], $input)->page($input["page"], $input["pageSize"])->select();
            return json(["status" => "success", "message" => "请求成功", "data" => $data, "count" => $count]);
        } else {
            return View::fetch();
        }
    }
    
    /**
     * 保存新建的资源
     */
    public function save()
    {
        if ($this->request->isPost()) {
            $input = input();
            '.$class.'Model::create($input);
            return json(["status" => "success", "message" => "添加成功"]);
        }
    }
    
    /**
     * 保存更新的资源
     */
    public function update()
    {
        if ($this->request->isPost()) {
            $input = input();
            $save  = '.$class.'Model::where("id", $input["id"])->update($input);
            $save->save();
            return json(["status" => "success", "message" => "修改成功"]);
        }
    }
    
    /**
     * 删除指定资源
     */
    public function delete()
    {
        if ($this->request->isPost()) {
            $input = input();
            '.$class.'Model::destroy($input["ids"]);
            return json(["status" => "success", "message" => "删除成功"]);
        }
    }
}';

// 生成模型
$model = '<?php
// +----------------------------------------------------------------------
// | OneKeyAdmin [ Believe that you can do better ]
// +----------------------------------------------------------------------
// | Copyright (c) 2020-2023 http://onekeyadmin.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: MUKE <513038996@qq.com>
// +----------------------------------------------------------------------
namespace app\admin\model;

use think\Model;

class '.$class.' extends Model
{
    // 搜索器
    public function searchKeywordAttr($query, $value)
    {
    	if (! empty($value)) {
	        $query->where("'.implode('|', $keywordField).'","like", "%" . $value . "%");
	    }
    }
}';

// 生成视图
$viewFieldStr = "[";
foreach ($viewField as $key => $val) {
    $comma = count($viewField) > $key ? "," : "";
    $viewFieldStr .= "\n\t\t\t\t\t{prop: '".$val['prop']."', label: '".$val['label']."', type: '".$val['type']."'}$comma";
}
$viewFieldStr .= "\n\t\t\t\t]";
$view = '{include file="$header" /}
<div class="mk-app-warp">
    <mk-table
        url="'.$path.'/index"
        save-url="'.$path.'/save"
        update-url="'.$path.'/update"
        delete-url="'.$path.'/delete"
        :date-search="false"
        :field="field">
    </mk-table>
</div>
{include file="$component" /}
<script>
    new Vue({
        el: "#app",
        data() {
            return {
                field: '.$viewFieldStr.',
            }
        },
    })
</script>
{include file="$footer" /}
';